
package com.sakurafish.example.simplegetter;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
/**
 * 日経のHTMLをただ表示するんだけどエミュレータだとものすごく時間がかかる
 * @author sakura
 *
 */
public class SpiderSimpleGetterActivity extends Activity {
    String TAG = "SpiderSimpleGetterActivity";
    String URL_1 = "http://www.nikkei.com";

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        TextView textView = (TextView) findViewById(R.id.TextView01);

        String html = null;

        try {
            // 引数からURLを読み込み、URLConnectionを開く
            URL url = new URL(URL_1);

            URLConnection urlConnection = url.openConnection();

            // InputStreamを取得し、効率の高いBufferedStreamを作成
            InputStream stream = urlConnection.getInputStream();
            InputStream buffer = new BufferedInputStream(stream);

            // 文字単位の入力であるReaderを作成、効率の高いBufferedReaderを作成
            Reader reader = new InputStreamReader(buffer);
            BufferedReader bufferedReader = new BufferedReader(reader);

            // １行ずつ読む。最後はnullが返る。
            String s = null;
            while ((s = bufferedReader.readLine()) != null) {
                html += s;
            }
            textView.setText(html);

        } catch (MalformedURLException e) {
            Log.e(TAG, html + "は正しいURLではありません:");
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
